package leetcodecompetition.D353;

import java.util.Arrays;

/**
 * @Author: Siantar
 * @Date: 2023-07-09-10:50
 * @Description: 1.0
 */
public class T6899 {
    public int maximumJumps(int[] nums, int target) {
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp, -1);
        int res = dp(dp, nums, target, 0);
        return res == -2 ? -1 : res;
    }

    public int dp(int[] dp, int[] nums, int target, int a) {
        if (a == nums.length - 1) return 0;
        if (dp[a] != -1) {
            return dp[a];
        }
        int res = -2;
        for (int i = a + 1; i < nums.length; i++) {
            if (Math.abs(nums[a] - nums[i]) <= target) {
                int d = dp(dp, nums, target, i);
                if (d != -2) {
                    res = Math.max(res, d + 1);
                }
            }
        }
        dp[a] = res;
        return res;
    }
}
